package com.guoguo.cloudmusic.ui

import android.content.Intent
import android.graphics.Color
import android.view.Gravity
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.bumptech.glide.Glide
import com.guoguo.cloudmusic.databinding.ActivityMainBinding
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.guoguo.cloudmusic.R
import com.guoguo.cloudmusic.logic.dao.UserInfoDao
import com.guoguo.cloudmusic.ui.common.Adapter.ViewPageFragmentAdapter
import com.guoguo.cloudmusic.ui.common.BaseActivity
import com.guoguo.cloudmusic.ui.home.find.FindFragment
import com.guoguo.cloudmusic.ui.home.mine.MineFragment
import com.guoguo.cloudmusic.ui.home.more.MoreFragment
import com.guoguo.cloudmusic.ui.login.LoginActivity
import com.guoguo.cloudmusic.ui.setting.SettingActivity
import com.guoguo.cloudmusic.utils.XToastUtils

class MainActivity : BaseActivity() {
    private var userInfo = UserInfoDao.getUserInfo()
    private lateinit var adapter: ViewPageFragmentAdapter
    private lateinit var viewPage: ViewPager
    private lateinit var bottomNavigationView: BottomNavigationView
    private var fragments: MutableList<Fragment> = ArrayList()
    private var _binding: ActivityMainBinding? = null
    private val binding: ActivityMainBinding
        get() = _binding!!

    fun openDraw() {
        val drawerLayout = binding.drawerLayout
        drawerLayout.openDrawer(Gravity.LEFT)
    }

    override fun initBinding() {
        super.initBinding()
        mainActivity = this
        _binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(_binding!!.root)
    }

    override fun initBar() {
        super.initBar()
        window.statusBarColor = Color.WHITE
    }

    /**
     * 设置 Fragment适配器
     */
    override fun initData() {
        super.initData()
        viewPage = binding.includeMain.viewPager
        bottomNavigationView = binding.includeMain.bottomNavigation
        fragments.apply {
            add(FindFragment())
            add(MoreFragment())
            add(MineFragment())
        }
        adapter = ViewPageFragmentAdapter(fragments, supportFragmentManager)
        viewPage.adapter = adapter
    }

    /**
     * 设置抽屉菜单界面
     */
    override fun initView() {
        super.initView()
        val navigationView = binding.navView
        val headerLayout = navigationView.getHeaderView(0)
        val headImg = headerLayout.findViewById(R.id.nav_header_img) as ImageView
        val name = headerLayout.findViewById(R.id.nav_header_username) as TextView
        val sign = headerLayout.findViewById(R.id.nav_header_sign) as TextView
        name.text = userInfo.profile.nickname
        sign.text = userInfo.profile.signature
        Glide.with(this).load(userInfo.profile.avatarUrl).into(headImg)
        navigationView.setNavigationItemSelectedListener {
            when (it.itemId) {
                R.id.nav_setting -> {
                    val intent = Intent(this, SettingActivity::class.java)
                    startActivity(intent)
                }
                R.id.nav_logoOut -> {
                    try {
                        UserInfoDao.logout()
                        val intent = Intent(
                            this,
                            LoginActivity::class.java
                        ).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
                        startActivity(intent)
                    } catch (e: Exception) {
                        XToastUtils.error("退出失败");
                    }
                }
                else -> XToastUtils.error("该功能尚未开发");
            }
            false
        }
    }

    override fun initListener() {
        super.initListener()
        //页面切换
        viewPage.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                bottomNavigationView.menu.getItem(position).isChecked = true
            }

            override fun onPageScrollStateChanged(state: Int) {
            }
        })
        //给bottomView设置导航切换监听
        bottomNavigationView.setOnNavigationItemSelectedListener {
            when (it.itemId) {
                R.id.nav_news -> viewPage.currentItem = 0
                R.id.nav_trending ->
                    viewPage.currentItem = 1
                R.id.nav_profile ->
                    viewPage.currentItem = 2
            }
            false
        }
        //退出登录
    }

    companion object {
        var mainActivity: MainActivity? = null
    }
}